<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\SysItem;
use yii\data\Sort;

/**
 * SysItemSearch represents the model behind the search form about `backend\models\SysItem`.
 */
class SysItemSearch extends SysItem
{
	public $class_name;
	public $brand_name;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['id', 'type', 'img_small_id', 'img_thumb_id', 'img_url_id', 'app_return', 'class_id', 'min_order_quantity'], 'integer'],
            [['name', 'tips', 'fabric_list', 'material_list', 'related_list','factory_code'], 'safe'],
            [['price_base', 'price_discount'], 'number'],
	        [['class_name','brand_name','update_at','unit_name','classname','brandname'],'safe'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
    	$login_id = Yii::$app->user->identity->id;
    	$user_type = Yii::$app->user->identity->user_type;
    	
    	if($user_type != 1){//非后台管理员
    		if(\Yii::$app->user->can('yys-view-platform')){
    			  $conditions = '1=1';	
    		}elseif(\Yii::$app->user->can('yys-view-all')){
    		    
	    		$admin_ids = AdminInfo::get_all_admin_by_operator($login_id); 		
	    		$conditions = "sys_item.admin_id in ($admin_ids) ";
	    		
	    	}elseif(\Yii::$app->user->can('yys-view-own')){
	    		$conditions = "sys_item.admin_id={$login_id}";
	    	}else{
	    		$conditions = '1 <> 1';
	    	}
    	}else{
    		$conditions = '1=1';
    	}
        $query = SysItem::find()->joinWith(['brand_name'])->joinWith(['class_name'])->joinWith("unit_name")->where($conditions)
        ->select("sys_item.*,grp_brand.name brandname,grp_class.name classname,grp_unit.name unit_name");
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
	        'pagination'=>['pageSize' => '10'],
            'sort' => ['defaultOrder' => ['id'=>SORT_DESC]]
        ]);
        $dataProvider->Sort->attributes['brand_name'] = [
                    'asc' => ['name' => SORT_ASC],
                    'desc' => ['name' => SORT_DESC],
                ];
        $dataProvider->Sort->attributes['class_name'] = [
            'asc' => ['name' => SORT_ASC],
            'desc' => ['name' => SORT_DESC],
        ]; 
  
        // $dataProvider->setSort([
        // 		'attributes' => [
        // 			'brand_name'=>[
	       //  			'asc' => ['name' =>SORT_ASC],
	       //  			'desc' => ['name' =>SORT_DESC],
	       //  			'label'=> Yii::t('app','Class Name')      		
        // 				],   
        // 			]   		
        // 		]);
        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        $query->andFilterWhere([
            'sys_item.id' => $this->id,
            'type' => $this->type,
            'img_small_id' => $this->img_small_id,
            'img_thumb_id' => $this->img_thumb_id,
            'img_url_id' => $this->img_url_id,
            'price_base' => $this->price_base,
            'price_discount' => $this->price_discount,
            'app_return' => $this->app_return,
            'grp_unit.name'=>$this->unit_name
            //'class_name' => $this->class_name,
            //'brand_id' => $this->brand_id,
        ]);
        $query->andFilterWhere(['like', 'sys_item.name', $this->name])
            ->andFilterWhere(['like', 'tips', $this->tips])
            ->andFilterWhere(['like', 'fabric_list', $this->fabric_list])
            ->andFilterWhere(['like', 'material_list', $this->material_list])
            ->andFilterWhere(['like', 'related_list', $this->related_list])
            ->andFilterWhere(['=', 'grp_brand.name', $this->brand_name])
            ->andFilterWhere(['like', 'grp_class.name', $this->class_name])
            ->andFilterWhere(['like', 'update_at', $this->update_at])
			->andFilterWhere(['like', 'factory_code', $this->factory_code]);

        return $dataProvider;
    }
    public function searchFront($params)
    {
        $query = SysItem::find()->joinWith(['brand_name'])->joinWith(['class_name']);
    
        $dataProvider = new ActiveDataProvider([
            'query' => $query,
            'pagination'=>['pageSize' => '10'],
            'sort'=>[
                'defaultOrder'=>[
                    'top_time'=>SORT_DESC,
                    'id'=>SORT_DESC
                ]
            ]
        ]);

        var_dump($this->load($params));
        print_r($params);
        if (!$this->validate()) {
            return $dataProvider;
        }
    
        $query->andFilterWhere([
            'sys_item.id' => $this->id,
            'type' => $this->type,
            'app_return' => 1,
            'class_id' => $this->class_id,
            'brand_id' => $this->brand_id,
            'min_order_quantity' => $this->min_order_quantity,
        ]);
        $query->andFilterWhere(['like', 'sys_item.name', $this->name])
        ->andFilterWhere(['like', 'tips', $this->tips])
        ->andFilterWhere(['like', 'related_list', $this->related_list])
        ->andFilterWhere(['like', 'grp_brand.name', $this->brand_name])
        ->andFilterWhere(['like', 'grp_class.name', $this->class_name])
        ->andFilterWhere(['like', 'factory_code', $this->factory_code]);
    
        return $dataProvider;
    }
}
